package com.retail.team.service.impl;


import com.retail.system.entity.vo.AdminVo;
import com.retail.system.mapper.AdminMapper;
import com.retail.team.entity.vo.RetailTeamAddSpVo;
import com.retail.core.entity.ParamReq;
import com.retail.team.entity.request.RetailTeamAddSpRequest;
import com.retail.team.enums.VerifyFlagEnums;
import com.retail.team.mapper.RetailTeamAddSpMapper;
import com.retail.team.service.RetailTeamAddSpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* 团队审核 Service impl
* @author Administrator
*/
@Service
public class RetailTeamAddSpServiceImpl implements RetailTeamAddSpService {

   @Resource
   private RetailTeamAddSpMapper retailTeamAddSpMapper;

   @Resource
   private AdminMapper adminMapper;

   @Override
   public RetailTeamAddSpVo getModel(Long id) {
      RetailTeamAddSpVo vo =   retailTeamAddSpMapper.getModel(id);
      vo.setVerifyFlagName(VerifyFlagEnums.getStatusValues(vo.getVerifyFlag()));
      AdminVo parentAdmin = adminMapper.getModel(vo.getParentId());
      vo.setAdminName(parentAdmin.getName());
      AdminVo admin = adminMapper.getModel(vo.getAdminId());
      vo.setAdminName(admin.getName());
      return vo;
   }

   @Transactional
   @Override
   public Long save(RetailTeamAddSpRequest retailTeamAddSpRequest) {
      retailTeamAddSpMapper.save(retailTeamAddSpRequest);
      if (retailTeamAddSpRequest.getVerifyFlag().equals(VerifyFlagEnums.SHEN_HE_TONGGUO.getValue())) {
         adminMapper.updateParenIdById(retailTeamAddSpRequest.getAdminId(),retailTeamAddSpRequest.getParentId());
      }
      return retailTeamAddSpRequest.getId();
   }

   @Transactional
   @Override
   public Long update(RetailTeamAddSpRequest retailTeamAddSpRequest) {
      retailTeamAddSpMapper.update(retailTeamAddSpRequest);

      if (retailTeamAddSpRequest.getVerifyFlag().equals(VerifyFlagEnums.SHEN_HE_TONGGUO.getValue())) {
         adminMapper.updateParenIdById(retailTeamAddSpRequest.getAdminId(),retailTeamAddSpRequest.getParentId());
      }
      return retailTeamAddSpRequest.getId();
   }

   @Override
   public List<RetailTeamAddSpVo> listPage(ParamReq<RetailTeamAddSpRequest> params) {
      params.setStartIndex((params.getCurrent() -1 ) * params.getSize());
      List<RetailTeamAddSpVo> res =  retailTeamAddSpMapper.listPage(params);
      res.forEach(r -> {
         r.setVerifyFlagName(VerifyFlagEnums.getStatusValues(r.getVerifyFlag()));
         AdminVo parentAdmin = adminMapper.getModel(r.getParentId());
         r.setAdminName(parentAdmin.getName());
         AdminVo admin = adminMapper.getModel(r.getAdminId());
         r.setAdminName(admin.getName());
      });

      return res;
   }

   @Override
   public Integer countPage(ParamReq<RetailTeamAddSpRequest> params) {
       return retailTeamAddSpMapper.countPage(params);
   }

   @Override
   public Integer updateDelFlagById(Long id, Integer delFlag) {
       return retailTeamAddSpMapper.updateDelFlagById(id, delFlag);
   }
}
